﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace CustSign
{
    public partial class frmCustDataUp : Form
    {
        OleDbConnection Conn;
        DataTable SexType;
        DataTable SignType;

        public frmCustDataUp()
        {
            InitializeComponent();
        }

        private void frmCustDataUp_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“dSCustData.CustList”中。您可以根据需要移动或移除它。
            this.custListTableAdapter.Fill(this.dSCustData.TCustList);

            Conn = new OleDbConnection();
            Conn.ConnectionString = Properties.Settings.Default.strOleConn;

            OleDbCommand Comm = Conn.CreateCommand();
            OleDbDataReader dgvColSetReader;
            Conn.Open();

            Comm.CommandText = "SELECT TypeValue, TypeName FROM TDictionary WHERE (TypeField = 'Sex') ORDER BY TypeOrder";
            dgvColSetReader = Comm.ExecuteReader();

            SexType = new DataTable();
            SexType.Load(dgvColSetReader);
            dgvColSetReader.Close();

            Comm.CommandText = "SELECT TypeValue, TypeName FROM TDictionary WHERE (TypeField = 'Sign') ORDER BY TypeOrder";
            dgvColSetReader = Comm.ExecuteReader();

            SignType = new DataTable();
            SignType.Load(dgvColSetReader);
            dgvColSetReader.Close();

            Comm.CommandText = "SELECT ID, FieldName, HeadName, FieldType, ColWidth, ColMinWidth, ColType, ReadOnly FROM TCols ORDER BY ID";

            dgvColSetReader = Comm.ExecuteReader();            

            while (dgvColSetReader.Read())
            {
                if ((String)dgvColSetReader["ColType"] == "txt")
                {
                    DataGridViewTextBoxColumn NewtxtCol = new DataGridViewTextBoxColumn();
                    NewtxtCol.DataPropertyName = (String)dgvColSetReader["FieldName"];
                    NewtxtCol.HeaderText = (String)dgvColSetReader["HeadName"];
                    NewtxtCol.Width = (Int32)dgvColSetReader["ColWidth"];
                    NewtxtCol.MinimumWidth = (Int32)dgvColSetReader["ColMinWidth"];
                    NewtxtCol.ReadOnly = Convert.ToBoolean(dgvColSetReader["ReadOnly"]);
                    dgvCustList.Columns.Add(NewtxtCol);
                }
                else if ((String)dgvColSetReader["ColType"] == "select")
                {
                    if ((String)dgvColSetReader["FieldName"] == "Sex")
                    {
                        DataGridViewComboBoxColumn NewSelCol = new DataGridViewComboBoxColumn();
                        NewSelCol.DataPropertyName = (String)dgvColSetReader["FieldName"];
                        NewSelCol.HeaderText = (String)dgvColSetReader["HeadName"];
                        NewSelCol.Width = (Int32)dgvColSetReader["ColWidth"];
                        NewSelCol.MinimumWidth = (Int32)dgvColSetReader["ColMinWidth"];

                        NewSelCol.DataSource = SexType;
                        NewSelCol.ValueMember = "TypeValue";
                        NewSelCol.DisplayMember = "TypeName";

                        dgvCustList.Columns.Add(NewSelCol);
                    }
                    else
                    {
                        DataGridViewComboBoxColumn NewSelCol = new DataGridViewComboBoxColumn();
                        NewSelCol.DataPropertyName = (String)dgvColSetReader["FieldName"];
                        NewSelCol.HeaderText = (String)dgvColSetReader["HeadName"];
                        NewSelCol.Width = (Int32)dgvColSetReader["ColWidth"];
                        NewSelCol.MinimumWidth = (Int32)dgvColSetReader["ColMinWidth"];
                        
                        NewSelCol.DataSource = SignType;
                        NewSelCol.ValueMember = "TypeValue";
                        NewSelCol.DisplayMember = "TypeName";
                        
                        dgvCustList.Columns.Add(NewSelCol);
                    }                    
                }                
            }

            dgvCustList.DataSource = this.custListBindingSource;

            Comm.Dispose();
            dgvColSetReader.Close();
            dgvColSetReader.Dispose();
            Conn.Close();
            Conn.Dispose();
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            custListBindingSource.EndEdit();
            this.custListTableAdapter.Update(this.dSCustData.TCustList);
            this.custListTableAdapter.Fill(this.dSCustData.TCustList);            
        }

        private void dgvCustList_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
        {
            if (e.StateChanged == DataGridViewElementStates.Selected)
            {
                dgvCustList.CurrentCell = e.Cell;
                dgvCustList.BeginEdit(false);
            }
        }
    }
}
